/*
提供在不同平台下的统一整数类型定义
目标：实现跨平台跨编译器的统一整数类型支持
*/

#ifndef EGUI_INT_H
#define EGUI_INT_H

#ifdef __cplusplus
extern "C" {
#endif

// 编译器和平台检测
#if defined(_MSC_VER)
    #define EGUI_COMPILER_MSVC
    #define EGUI_COMPILER_VERSION _MSC_VER
#elif defined(__GNUC__)
    #define EGUI_COMPILER_GCC
    #define EGUI_COMPILER_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#elif defined(__clang__)
    #define EGUI_COMPILER_CLANG
    #define EGUI_COMPILER_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#elif defined(__BORLANDC__)
    #define EGUI_COMPILER_BORLAND
    #define EGUI_COMPILER_VERSION __BORLANDC__
#elif defined(__INTEL_COMPILER)
    #define EGUI_COMPILER_INTEL
    #define EGUI_COMPILER_VERSION __INTEL_COMPILER
#else
    #define EGUI_COMPILER_UNKNOWN
    #define EGUI_COMPILER_VERSION 0
#endif

// 平台检测
#if defined(_WIN32) || defined(_WIN64)
    #define EGUI_PLATFORM_WINDOWS
#elif defined(__linux__)
    #define EGUI_PLATFORM_LINUX
#elif defined(__APPLE__) && defined(__MACH__)
    #define EGUI_PLATFORM_MACOS
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
    #define EGUI_PLATFORM_BSD
#elif defined(__sun)
    #define EGUI_PLATFORM_SOLARIS
#else
    #define EGUI_PLATFORM_UNKNOWN
#endif

// 架构检测
#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
    #define EGUI_ARCH_X64
    #define EGUI_ARCH_64BIT
#elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
    #define EGUI_ARCH_X86
    #define EGUI_ARCH_32BIT
#elif defined(_M_ARM64) || defined(__aarch64__)
    #define EGUI_ARCH_ARM64
    #define EGUI_ARCH_64BIT
#elif defined(_M_ARM) || defined(__arm__) || defined(__thumb__)
    #define EGUI_ARCH_ARM
    #define EGUI_ARCH_32BIT
#else
    #define EGUI_ARCH_UNKNOWN
#endif

// 检测是否支持标准的stdint.h
#ifndef _STDINT_HEADER_NOT_SUPPORT
    #if defined(EGUI_COMPILER_MSVC) && (EGUI_COMPILER_VERSION < 1600)  // MSVC < 2010
        #define _STDINT_HEADER_NOT_SUPPORT
    #elif defined(EGUI_COMPILER_BORLAND) && (__STDC_VERSION__ < 199901L)
        #define _STDINT_HEADER_NOT_SUPPORT
    #elif defined(EGUI_COMPILER_GCC) && (EGUI_COMPILER_VERSION < 30300)  // GCC < 3.3
        #define _STDINT_HEADER_NOT_SUPPORT
    #elif defined(EGUI_COMPILER_CLANG) && (__clang_major__ < 3)
        #define _STDINT_HEADER_NOT_SUPPORT
    #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ < 199901L)  // C99之前
        #define _STDINT_HEADER_NOT_SUPPORT
    // 对于其他未知编译器，默认尝试使用stdint.h
    #endif
#endif

// 使用标准stdint.h或自定义实现
#if !defined(_STDINT_HEADER_NOT_SUPPORT)
    #include <stdint.h>
    #ifndef EGUI_HAS_STDINT_H
        #define EGUI_HAS_STDINT_H
    #endif
#else
    // 自定义整数类型定义
    typedef signed char int8_t;
    typedef unsigned char uint8_t;
    typedef signed short int int16_t;
    typedef unsigned short uint16_t;
    typedef signed int int32_t;
    typedef unsigned int uint32_t;
    
    // 64位整数支持
    #if !defined(EGUI_NO_64_INT)
        #if defined(EGUI_COMPILER_MSVC)
            typedef __int64 int64_t;
            typedef unsigned __int64 uint64_t;
        #elif defined(EGUI_COMPILER_GCC) || defined(EGUI_COMPILER_CLANG)
            typedef long long int64_t;
            typedef unsigned long long uint64_t;
        #elif defined(EGUI_COMPILER_BORLAND)
            #ifdef EGUI_64_INT_TYPE
                typedef EGUI_64_INT_TYPE int64_t;
                typedef unsigned EGUI_64_INT_TYPE uint64_t;
            #else
                #error "您必须定义64位整数的实际类型 (Define EGUI_64_INT_TYPE)"
            #endif
        #else
            #error "当前编译器未定义64位整数支持"
        #endif
    #endif
    
    // 指针相关整数类型
    #if defined(EGUI_ARCH_64BIT)
        typedef int64_t intptr_t;
        typedef uint64_t uintptr_t;
        #define INTPTR_MIN (-9223372036854775807LL - 1)
        #define INTPTR_MAX 9223372036854775807LL
        #define UINTPTR_MAX 18446744073709551615ULL
    #else
        typedef int32_t intptr_t;
        typedef uint32_t uintptr_t;
        #define INTPTR_MIN (-2147483647 - 1)
        #define INTPTR_MAX 2147483647
        #define UINTPTR_MAX 4294967295U
    #endif
    
    // 整数常量宏
    #define INT8_MIN (-127 - 1)
    #define INT8_MAX 127
    #define UINT8_MAX 255U
    #define INT16_MIN (-32767 - 1)
    #define INT16_MAX 32767
    #define UINT16_MAX 65535U
    #define INT32_MIN (-2147483647 - 1)
    #define INT32_MAX 2147483647
    #define UINT32_MAX 4294967295U
    
    #if !defined(EGUI_NO_64_INT)
        #define INT64_MIN (-9223372036854775807LL - 1)
        #define INT64_MAX 9223372036854775807LL
        #define UINT64_MAX 18446744073709551615ULL
    #endif
    
    // 指针差值类型
    typedef intptr_t ptrdiff_t;
    #define PTRDIFF_MIN INTPTR_MIN
    #define PTRDIFF_MAX INTPTR_MAX
    
    // 最大尺寸类型
    #if defined(EGUI_ARCH_64BIT)
        typedef int64_t intmax_t;
        typedef uint64_t uintmax_t;
        #define INTMAX_MIN INT64_MIN
        #define INTMAX_MAX INT64_MAX
        #define UINTMAX_MAX UINT64_MAX
    #else
        typedef int32_t intmax_t;
        typedef uint32_t uintmax_t;
        #define INTMAX_MIN INT32_MIN
        #define INTMAX_MAX INT32_MAX
        #define UINTMAX_MAX UINT32_MAX
    #endif
    
    // 常量宏
    #define INT8_C(val) (val)
    #define UINT8_C(val) (val ## U)
    #define INT16_C(val) (val)
    #define UINT16_C(val) (val ## U)
    #define INT32_C(val) (val)
    #define UINT32_C(val) (val ## U)
    
    #if !defined(EGUI_NO_64_INT)
        #if defined(EGUI_COMPILER_MSVC)
            #define INT64_C(val) (val ## i64)
            #define UINT64_C(val) (val ## ui64)
        #else
            #define INT64_C(val) (val ## LL)
            #define UINT64_C(val) (val ## ULL)
        #endif
    #endif
    
    #define INTMAX_C(val) INT64_C(val)
    #define UINTMAX_C(val) UINT64_C(val)
    
    // size_t相关定义
    #ifndef SIZE_MAX
        #if defined(EGUI_ARCH_64BIT)
            #define SIZE_MAX UINT64_MAX
        #else
            #define SIZE_MAX UINT32_MAX
        #endif
    #endif
#endif

// 额外的EGUI特定整数类型别名，提高代码可读性
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;

#if !defined(EGUI_NO_64_INT)
    typedef uint64_t u64;
    typedef int64_t i64;
#endif

// 确保类型大小正确
#define EGUI_STATIC_ASSERT(expr, msg) typedef char static_assertion_##msg[(expr) ? 1 : -1]

EGUI_STATIC_ASSERT(sizeof(int8_t) == 1, int8_t_size_must_be_1);
EGUI_STATIC_ASSERT(sizeof(uint8_t) == 1, uint8_t_size_must_be_1);
EGUI_STATIC_ASSERT(sizeof(int16_t) == 2, int16_t_size_must_be_2);
EGUI_STATIC_ASSERT(sizeof(uint16_t) == 2, uint16_t_size_must_be_2);
EGUI_STATIC_ASSERT(sizeof(int32_t) == 4, int32_t_size_must_be_4);
EGUI_STATIC_ASSERT(sizeof(uint32_t) == 4, uint32_t_size_must_be_4);

#if !defined(EGUI_NO_64_INT)
    EGUI_STATIC_ASSERT(sizeof(int64_t) == 8, int64_t_size_must_be_8);
    EGUI_STATIC_ASSERT(sizeof(uint64_t) == 8, uint64_t_size_must_be_8);
#endif

#ifdef __cplusplus
}

// C++特定扩展
namespace ui {
    // 使用标准整数类型作为ui命名空间内的类型
    using int8_t = ::int8_t;
    using uint8_t = ::uint8_t;
    using int16_t = ::int16_t;
    using uint16_t = ::uint16_t;
    using int32_t = ::int32_t;
    using uint32_t = ::uint32_t;
    
    #if !defined(EGUI_NO_64_INT)
        using int64_t = ::int64_t;
        using uint64_t = ::uint64_t;
    #endif
    
    using intptr_t = ::intptr_t;
    using uintptr_t = ::uintptr_t;
    using size_t = ::size_t;
    using ptrdiff_t = ::ptrdiff_t;
    using intmax_t = ::intmax_t;
    using uintmax_t = ::uintmax_t;
    
    // 简化的类型别名
    using u8 = uint8_t;
    using i8 = int8_t;
    using u16 = uint16_t;
    using i16 = int16_t;
    using u32 = uint32_t;
    using i32 = int32_t;
    
    #if !defined(EGUI_NO_64_INT)
        using u64 = uint64_t;
        using i64 = int64_t;
    #endif
}
#endif

#endif // EGUI_INT_H